// 题目：有100个请求需要发送，并发数量为10，直到请求完这个100个请求
/**
 * 
 * @param urls 所有请求的url
 * @param maxNum 并发数量
 * @returns 
 */
function concurrentRequest(urls: string[], maxNum: number): Promise<any[]> {
  if (urls.length === 0) return Promise.resolve([]);
  return new Promise((resolve) => {
    const response: any[] = new Array(urls.length);
    let requestIndex = 0;
    const url = urls[requestIndex];
    async function _request() {
      if (requestIndex >= urls.length) {
        resolve(response);
        return;
      }
      const res = await fetch(url);
      response[requestIndex] = { url, data: [] };
      requestIndex++;
      _request();
    }
    for (let index = 0; index < Math.min(maxNum, urls.length); index++) {
      _request();
    }
  });
}

async function test() {
  const urls = new Array(60).fill(
    "https://middleground-test.readboy.com/api/v2/person/role/list?school_id=3711740001268044&page=1&page_size=100&role_code=role%7C1012"
  );
  const responseList = await concurrentRequest(urls, 10);
  console.log("response: ", responseList);
}
test();
